//
//  DAPersonalBorrowBookStatusView.m
//  DAKnowledge
//
//  Created by DreamArts on 16/5/4.
//  Copyright © 2016年 lidongyang. All rights reserved.
//

#import "DAPersonalBorrowBookStatusView.h"
#import "DALabel.h"
#import "Masonry.h"
#import "Utility.h"
#import "DAHeader.h"
#import "UIImageView+WebCache.h"
@implementation DAPersonalBorrowBookStatusView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithHexString:backGroundColor];
        /** bookImage */
        self.bookImageView = [[UIImageView alloc]init];
        [self addSubview:self.bookImageView];
        /** bookName */
        self.bookNameLabel = [[DALabel alloc]init];
        [self addSubview:self.bookNameLabel];
        /** bookAuthor */
        self.bookAuthor = [[DALabel alloc]init];
         [self addSubview:self.bookAuthor];
        /** bookType */
        self.bookType = [[DALabel alloc]init];
        self.bookType.font = [UIFont systemFontOfSize:12 * (self.bounds.size.width / 375)];
        self.bookType.verticalAlignment = VerticalAlignmentBottom;
        [self addSubview:self.bookType];
        /** bookHolder */
        self.bookHolder = [[DALabel alloc]init];
        self.bookHolder.font = [UIFont systemFontOfSize:12 * (self.bounds.size.width / 375)];
        self.bookHolder.verticalAlignment = VerticalAlignmentBottom;
        [self addSubview:self.bookHolder];
        /** bookStatus */
        self.bookStatus = [[DALabel alloc]init];
        self.bookStatus.font = [UIFont systemFontOfSize:16 * (self.bounds.size.width / 375) ];
        self.bookStatus.textColor = [UIColor supportGreenColor];
        self.bookStatus.textAlignment = NSTextAlignmentRight;
        self.bookStatus.verticalAlignment = VerticalAlignmentBottom;
        [self addSubview:self.bookStatus];
        /** backButton */
        NSString *backBook = NSLocalizedString(@"WantBackBook", nil);
        
        self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.backButton setTitle:backBook forState:UIControlStateNormal];
        self.backButton.backgroundColor = [UIColor supportGreenColor];
        self.backButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.backButton.layer.cornerRadius = 5;
        [self.backButton addTarget:self action:@selector(clickBackButton)
                  forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.backButton];
        /** delayButton */
        NSString *delayButton = NSLocalizedString(@"delayTime", nil);
        
        self.delayButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.delayButton setTitle:delayButton forState:UIControlStateNormal];
        self.delayButton.backgroundColor = [UIColor supportGreenColor];
        self.delayButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.delayButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.delayButton.layer.cornerRadius = 5;
        [self.delayButton addTarget:self action:@selector(clickDelayButton)
                   forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.delayButton];
        /** lostButton */
        NSString *lostButton = NSLocalizedString(@"WantLostBook", nil);
        
        self.lostButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.lostButton setTitle:lostButton forState:UIControlStateNormal];
        self.lostButton.backgroundColor = [UIColor colorWithHexString:lostButtonColor];
        self.lostButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.lostButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.lostButton.layer.cornerRadius = 5;
        [self.lostButton addTarget:self action:@selector(clickLostButton)
                  forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.lostButton];
        /** 左上角 图片 */
        self.disMissImage = [[UIImageView alloc]init];
        self.disMissImage.image = [UIImage imageNamed:@"close"];
        [self addSubview:self.disMissImage];
        self.disMissImage.userInteractionEnabled = YES;
        /** 给图片添加手势*/
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickCancelImage:)];
        [self.disMissImage addGestureRecognizer:tap];
        /** star 1 */
        self.starOneImage = [[UIImageView alloc]init];
        [self addSubview:self.starOneImage];
        /** star 2 */
        self.starTwoImage = [[UIImageView alloc]init];
        [self addSubview:self.starTwoImage];
        /** star 3 */
        self.starThreeImage = [[UIImageView alloc]init];
        [self addSubview:self.starThreeImage];
        /** star 4 */
        self.starFourImage = [[UIImageView alloc]init];
        [self addSubview:self.starFourImage];
        /** star 5 */
        self.starFiveImage = [[UIImageView alloc]init];
        [self addSubview:self.starFiveImage];
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 5;
        
//        self.rateView = [[DARateView alloc]init];
//        self.rateView.backgroundColor = [UIColor redColor];
//        [self addSubview:self.rateView];
        
        //[self creatView];
    }
    
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    [self creatView];
}
#pragma mark - 布局
- (void)creatView{
    /** 书的图片的布局 */
    [self.bookImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(30 * (self.bounds.size.width / 375));
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(110 * self.bounds.size.width / 375);
        make.height.mas_equalTo(140 * (self.bounds.size.width / 375));
    }];
    /** 书名的布局 */
    [self.bookNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bookImageView);
        make.left.mas_equalTo(self.bookImageView.mas_right).offset(10);
        make.right.mas_equalTo(self.mas_right).offset(-20);
        make.bottom.mas_equalTo(self.starOneImage.mas_top).offset(-5);
    }];
    
//    /** 作者的布局 */
//    [self.bookAuthor mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.bookNameLabel.mas_bottom).offset(5);
//        make.left.mas_equalTo(self.bookNameLabel);
//        make.right.mas_equalTo(self.bookNameLabel);
//        make.bottom.mas_equalTo(self.starOneImage.mas_top).offset(-5);
//    }];
   /** statr 1 */
    [self.starOneImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bookNameLabel.mas_bottom).offset(5);
        make.left.mas_equalTo(self.bookNameLabel);
        make.width.mas_equalTo(12);
        make.height.mas_equalTo(12);
    }];
    /** star 2 */
    [self.starTwoImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.starOneImage);
        make.left.mas_equalTo(self.starOneImage.mas_right).offset(1);
        make.width.height.mas_equalTo(self.starOneImage);
    }];
    /** star 3 */
    [self.starThreeImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.starOneImage);
        make.left.mas_equalTo(self.starTwoImage.mas_right).offset(1);
        make.width.height.mas_equalTo(self.starOneImage);
    }];
    /** star 4 */
    [self.starFourImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.starOneImage);
        make.left.mas_equalTo(self.starThreeImage.mas_right).offset(1);
        make.width.height.mas_equalTo(self.starOneImage);
    }];
    /** star 5 */
    [self.starFiveImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.starOneImage);
        make.left.mas_equalTo(self.starFourImage.mas_right).offset(1);
        make.width.height.mas_equalTo(self.starOneImage);
    }];
//
//    [self.rateView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.bookNameLabel.mas_bottom).offset(5);
//        make.left.mas_equalTo(self.bookNameLabel);
//        make.width.mas_equalTo(64);
//        make.height.mas_equalTo(12);
//    }];
//    

    /** 书的状态 */
    [self.bookStatus mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.bookImageView.mas_bottom).offset(0);
        make.right.mas_equalTo(self.mas_right).offset(-20);
        make.left.mas_equalTo(self.bookHolder.mas_right).offset(8);
       // make.height.mas_equalTo(30);
    }];
    /** 书的持有人 */
    [self.bookHolder mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.bookImageView);
        make.left.mas_equalTo(self.bookNameLabel);
      //  make.height.mas_equalTo(20);
        //make.width.mas_equalTo(100);
        make.right.mas_equalTo(self.bookStatus.mas_left).offset(-8);
    }];
    /** 书的类型 */
    [self.bookType mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.bookHolder.mas_top).offset(-5);
        make.left.mas_equalTo(self.bookHolder);
        make.width.mas_equalTo(self.bookHolder);
    }];
    /** 消失按钮 */
    [self.disMissImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-5);
        make.top.mas_equalTo(5);
        make.width.height.mas_equalTo(20);
    }];
    /** 归还按钮 */
    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bookImageView.mas_bottom).offset(20);
        make.left.mas_equalTo(self.bookImageView);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(30);
    }];
    /** 延期按钮 */
    [self.delayButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backButton.mas_bottom).offset(10);
        make.left.right.width.height.mas_equalTo(self.backButton);
    }];
    /** 报失按钮 */
    [self.lostButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.delayButton.mas_bottom).offset(10);
        make.left.right.width.height.mas_equalTo(self.delayButton);
    }];
    self.bookNameLabel.font = [UIFont systemFontOfSize:18 * (self.bounds.size.width / 375)];
    self.bookNameLabel.verticalAlignment = VerticalAlignmentTop;
    self.bookAuthor.font = [UIFont systemFontOfSize:12 * (self.bounds.size.width / 375)];
   // self.bookAuthor.verticalAlignment = VerticalAlignmentTop;
}
#pragma mark - 点击了归还按钮
- (void)clickBackButton{
    NSLog(@"归还图书");
    [self.delegate backBookWithBorrowID:self.personalBookManageModel.borrowID];
}
#pragma mark - 点击了延期按钮
- (void)clickDelayButton{
    NSLog(@"延期");
    [self.delegate bookDelayWithBorrowID:self.personalBookManageModel.borrowID];
}
#pragma mark - 点击了报失按钮
- (void)clickLostButton{
    NSLog(@"报失");
    [self.delegate bookLost:self.personalBookManageModel.borrowID];
}
#pragma mark - 点击左上角取消图片
- (void)clickCancelImage:(UITapGestureRecognizer *)tap{
    NSLog(@"点击了取消");
    [self.delegate clickCancel];
}

- (void)setPersonalBookManageModel:(DAPersonalBookManageModel *)personalBookManageModel{
    
    if (_personalBookManageModel != personalBookManageModel) {
        _personalBookManageModel = personalBookManageModel;
    }
    
    UIView *bookCover = [[UIView alloc]initWithFrame:CGRectMake(0 , 0, self.bookImageView.frame.size.width, self.bookImageView.frame.size.height)];
    bookCover.backgroundColor = [UIColor supportGreenColor];
    DALabel *bookCoverName = [[DALabel alloc]initWithFrame:CGRectMake(5, 5, bookCover.frame.size.width -10, bookCover.frame.size.height - 10)];
    [bookCoverName setText:personalBookManageModel.bookName fitToContent:YES];
    bookCoverName.verticalAlignment = VerticalAlignmentMiddle;
    bookCoverName.textAlignment = NSTextAlignmentCenter;
    [bookCoverName setTextColor:[UIColor whiteColor]];
    bookCoverName.font = [UIFont systemFontOfSize:12];
    [bookCover addSubview:bookCoverName];
    
    UIImage *image = [self makeImageWithView:bookCover];
    [self.bookImageView sd_setImageWithURL:[NSURL URLWithString:personalBookManageModel.bookImage] placeholderImage:image];

    //[self.bookImageView sd_setImageWithURL:[NSURL URLWithString:personalBookManageModel.bookImage]];
    NSString *bookName = personalBookManageModel.bookName;
    [self.bookNameLabel setText:bookName fitToContent:YES];
    self.bookNameLabel.textColor = [UIColor colorWithHexString:bookNameColor];
    [self bookRate:personalBookManageModel.bookRate];
    NSString *bookTypeStr = NSLocalizedString(@"bookType", nil);
    NSString *bookType = [NSString stringWithFormat:@"%@: %@", bookTypeStr,personalBookManageModel.bookTypeName];
    [self.bookType setText:bookType fitToContent:YES];
    self.bookType.font = [UIFont systemFontOfSize:12];
    self.bookType.verticalAlignment = VerticalAlignmentBottom;
    self.bookType.textColor = [UIColor colorWithHexString:borrowTimeColor];
    NSString *bookSourceStr = NSLocalizedString(@"bookSource", nil);
    NSString *bookSource = [NSString stringWithFormat:@"%@: %@", bookSourceStr,personalBookManageModel.bookSourceName];
    [self.bookHolder setText:bookSource fitToContent:YES];
    self.bookHolder.font = [UIFont systemFontOfSize:12];
    self.bookHolder.textColor = [UIColor colorWithHexString:borrowTimeColor];
    self.bookHolder.verticalAlignment = VerticalAlignmentBottom;
    self.backButton.userInteractionEnabled = YES;
//    
//    if ([self.bookStatus.text isEqualToString:@"已归还"]) {
//        self.backButton.backgroundColor = [UIColor colorWithHexString:backNowColor];
//        self.backButton.userInteractionEnabled = NO;
//    }else{
//        self.backButton.userInteractionEnabled = YES;
//        self.backButton.backgroundColor = [UIColor supportGreenColor];
//    }
    
  
}

#pragma mark - 判断评分
- (void)bookRate:(NSString *)rateNumber{
    
    if ([rateNumber isEqualToString:@"0"]) {
        self.starOneImage.image = [UIImage imageNamed:@"rate0@3x"];
        self.starTwoImage.image = [UIImage imageNamed:@"rate0@3x"];
        self.starThreeImage.image = [UIImage imageNamed:@"rate0@3x"];
        self.starFourImage.image = [UIImage imageNamed:@"rate0@3x"];
        self.starFiveImage.image = [UIImage imageNamed:@"rate0@3x"];
    } else if ([rateNumber isEqualToString:@"1"]){
        self.starOneImage.image = [UIImage imageNamed:@"rate1@3x"];
        self.starTwoImage.image = [UIImage imageNamed:@"rate0@3x"];
        self.starThreeImage.image = [UIImage imageNamed:@"rate0@3x"];
        self.starFourImage.image = [UIImage imageNamed:@"rate0@3x"];
        self.starFiveImage.image = [UIImage imageNamed:@"rate0@3x"];
    }else if ([rateNumber isEqualToString:@"2"]){
        self.starOneImage.image = [UIImage imageNamed:@"rate1@3x"];
        self.starTwoImage.image = [UIImage imageNamed:@"rate1@3x"];
        self.starThreeImage.image = [UIImage imageNamed:@"rate0@3x"];
        self.starFourImage.image = [UIImage imageNamed:@"rate0@3x"];
        self.starFiveImage.image = [UIImage imageNamed:@"rate0@3x"];
    }else if ([rateNumber isEqualToString:@"3"]){
        self.starOneImage.image = [UIImage imageNamed:@"rate1@3x"];
        self.starTwoImage.image = [UIImage imageNamed:@"rate1@3x"];
        self.starThreeImage.image = [UIImage imageNamed:@"rate1@3x"];
        self.starFourImage.image = [UIImage imageNamed:@"rate0@3x"];
        self.starFiveImage.image = [UIImage imageNamed:@"rate0@3x"];
    }else if ([rateNumber isEqualToString:@"4"]){
        self.starOneImage.image = [UIImage imageNamed:@"rate1@3x"];
        self.starTwoImage.image = [UIImage imageNamed:@"rate1@3x"];
        self.starThreeImage.image = [UIImage imageNamed:@"rate1@3x"];
        self.starFourImage.image = [UIImage imageNamed:@"rate1@3x"];
        self.starFiveImage.image = [UIImage imageNamed:@"rate0@3x"];
    }else if ([rateNumber isEqualToString:@"5"]){
        self.starOneImage.image = [UIImage imageNamed:@"rate1@3x"];
        self.starTwoImage.image = [UIImage imageNamed:@"rate1@3x"];
        self.starThreeImage.image = [UIImage imageNamed:@"rate1@3x"];
        self.starFourImage.image = [UIImage imageNamed:@"rate1@3x"];
        self.starFiveImage.image = [UIImage imageNamed:@"rate1@3x"];
    }
    
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
